package com.hnust.service.impl;

import com.hnust.dao.GetAnswerDao;
import com.hnust.domain.QuestionAnswers;
import com.hnust.service.GetAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("GetAnswerService")
public class GetAnswerServiceImpl implements GetAnswerService {             //简答题也有可能一道题多个图片url
    @Autowired
    private GetAnswerDao getAnswerDao;
    @Override
    public QuestionAnswers getAnswer(String testId, String questionId, String studentId, String questionType, String number) {
          QuestionAnswers questionAnswers = getAnswerDao.getQuestionMsg(questionId);    //获取题目的信息
          ArrayList<String> answer = getAnswerDao.getCorrectAnswer(questionId);   //获取正确答案
        System.out.println("改之前的正确answer");
        System.out.println(answer);
        for (int i = 0; i < answer.size(); i++) {
           answer.set(i,answer.get(i).substring(3, answer.get(i).length() - 4).replaceAll("&nbsp"," ")) ; //对答案进行截取替换功能
        }
        System.out.println("改之后的正确answer");
        System.out.println(answer);
        questionAnswers.setAnswer(answer);         //将处理好的正确答案放入指定对象

        //将处理好的题目内容、解析放入指定对象中
        String text = questionAnswers.getText().replaceAll("&nbsp"," "); //将题目内容字符串里的&nbsp替换成空格
        String analysis = questionAnswers.getAnalysis().substring(3,questionAnswers.getAnalysis().length() - 4);//截取题目解析
        analysis = analysis.replaceAll("&nbsp"," "); //将字符串里的&nbsp替换成空格
        questionAnswers.setText(text);
        questionAnswers.setAnalysis(analysis);

          String stuAnswer = getAnswerDao.getStuAnswer(testId,studentId);  //获取学生答案串
          String [] stuAnswerType = stuAnswer.split("@fg@");  //将result按照题目类型分割开
           List<ArrayList<String>> list = new ArrayList<>();      //将所有答案拆开放入list中

        for (int i = 0; i < stuAnswerType.length; i++) {
            //选择题处理部分
            if(stuAnswerType[i].contains("@￥#1@")) {
                String [] optionType = stuAnswerType[i].split("@￥#1@");
                for (String s : optionType) {
                    ArrayList<String> optionAnswer = new ArrayList<>();
                    if (s.equals("(?)")) {          //如果字符串是（？）,代表学生该答案未填写
                        optionAnswer.add(" ");
                    } else {
                        optionAnswer.add(s);
                    }
                    list.add(optionAnswer);
                }
            }
            //判断题处理部分
            else if (stuAnswerType[i].contains("@￥#2@")) {
                String [] judgeType = stuAnswerType[i].split("@￥#2@");
                for (String s : judgeType) {
                    ArrayList<String> JudgeAnswer = new ArrayList<>();
                    if (s.equals("(?)")) {          //如果字符串是（？）,代表学生该答案未填写
                        JudgeAnswer.add(" ");
                    } else {
                        JudgeAnswer.add(s);
                    }
                    list.add(JudgeAnswer);
                }
            }
            //填空题处理部分
            else if (stuAnswerType[i].contains("@￥#3@")) {
                String [] completionType = stuAnswerType[i].split("@￥#3@");
                for (String s : completionType) {
                    String s1 = "";
                    if (s.contains("#1!#")) {   //该填空题有多个空的情况
                        String[] completionPartAnswer = s.split("#1!#");
                        for (int k = 0; k < completionPartAnswer.length; k++) {
                            if (k == 0) {
                                if (completionPartAnswer[k].equals("(?)")) s1 = s1 + " ";
                                else s1 = s1 + completionPartAnswer[k];
                            } else {
                                if (completionPartAnswer[k].equals("(?)")) s1 = s1 + "， ";
                                else s1 = s1 + "," + completionPartAnswer[k];
                            }
                        }
                    } else {   //该填空题只有一个空的情况
                        if (s.equals("(?)")) s1 = " ";
                        else s1 = s;
                    }
                    ArrayList<String> completionAnswer = new ArrayList<>();
                    completionAnswer.add(s1);
                    list.add(completionAnswer);
                }
            }
            //简答题处理部分
            else if (stuAnswerType[i].contains("@￥#4@")) {
                String [] shortType = stuAnswerType[i].split("@￥#4@");
                for (String s : shortType) {
                    if (s.contains(",")) {
                        String[] shortAnswer_part = s.split(",");  //将简答题多个图片的url分割开
                        ArrayList<String> short_Answer = new ArrayList<>(Arrays.asList(shortAnswer_part)); //转换为arraylist
                        list.add(short_Answer);
                    } else if (s.equals("(?)")) {
                        ArrayList<String> short_Answer = new ArrayList<>();
                        short_Answer.add(" ");
                        list.add(short_Answer);
                    } else {
                        ArrayList<String> short_Answer = new ArrayList<>();
                        short_Answer.add(shortType[i]);
                        list.add(short_Answer);
                    }
                }
            }
        }
        System.out.println(list);
         ArrayList<String> studentAnswer = list.get(Integer.parseInt(number)-1);  //根据题目序号提取相应的学生答案

           //将处理好的学生答案放入指定对象中
          questionAnswers.setStuAnswer(studentAnswer);

          System.out.println("获取的题目内容如下");
          System.out.println(questionAnswers);

          if(questionType.equals("选择题")) {
              ArrayList<String> options = new ArrayList<>();
              String[] optionMsg = getAnswerDao.getOptionQuestionMsg(questionId);
              for (String s : optionMsg) {
                  String optionContent = s.substring(3, s.length() - 4);   //截取选择题各选项内容
                  options.add(optionContent);                              //用列表接收
              }
              questionAnswers.setOptions(options);                         //将接收好的列表放入对象
          }
            System.out.println("装填好选项内容的内容如下");
            System.out.println(questionAnswers);
            return questionAnswers;
    }
}
